package com.diors.commons.log;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * 框架Log基类
 * @author	darren.ouyang
 * @data	2013-9-9
 */
public class DiorsLogger 
{
	
	/* log4j实例 */
	private Logger log;
	
	private DiorsLogger (Logger log)
	{
		this.log = log;
	}
	
	/**
	 * 开启游戏配置log文件位置
	 * @param path	配置文件位置
	 * @create	2013-9-9	darren.ouyang
	 */
	public static void configure (String path)
	{
		PropertyConfigurator.configure(path);
	}

	/**
	 * 对传入对象初始化log
	 * @param clz	传入所需对象的class对象
	 * @return
	 * @create	2013-9-9	darren.ouyang
	 */
	public static DiorsLogger getLogger(Class<? extends Object> clz)
	{
		return new DiorsLogger(Logger.getLogger(clz));
	}
	
	/**
	 * 检验log4j是否启动
	 * @param str
	 * @param objs
	 * @return
	 * @create	2013-9-4	darren.ouyang
	 */
	private boolean checkLog4j (String str, Object... objs)
	{
		if (log != null)
			return false;
		
		System.out.println(new StringBuilder("启用临时Log ===>").append(str).toString());
		if (objs != null)
		{
			for (Object obj : objs)
			{
				if (obj instanceof Throwable)
				{
					((Throwable)obj).printStackTrace();
				}
				else
				{
					System.out.println(obj);
				}
			}
		}
		
		return true;
	}
	
	public void debug(String str)
	{
		if (checkLog4j(str)) return;
		log.debug(str);
	}
	
	public void debug(String str, Throwable e)
	{
		if (checkLog4j(str, e)) return;
		log.debug(str, e);
	}
	
	public void error(String str)
	{
		if (checkLog4j(str)) return;
		log.error(str);
	}
	
	public void error(String str, Throwable e)
	{
		if (checkLog4j(str, e)) return;
		log.error(str, e);
	}
	
	public void info(String str)
	{
		if (checkLog4j(str)) return;
		log.info(str);
	}
	public void info(String str, Throwable e)
	{
		if (checkLog4j(str, e)) return;
		log.info(str, e);
	}
	
	public void warn(String str)
	{
		if (checkLog4j(str)) return;
		log.warn(str);
	}
	
	public void warn(String str, Throwable e)
	{
		if (checkLog4j(str, e)) return;
		log.warn(str, e);
	}

}
